package uicomponents;

import java.io.IOException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class UISlider extends UIComponentBase {
    @Override
    public void encodeEnd(FacesContext context) throws IOException
    {
        String id = (String)getAttributes().get("id");
        String value = (String)getAttributes().get("value");
        String minimumValue = (String)getAttributes().get("minimumValue");
        String maximumValue = (String)getAttributes().get("maximumValue");
        String trackWidth = (String)getAttributes().get("trackWidth");
        String trackHeight = (String)getAttributes().get("trackHeight");
        String trackColor = (String)getAttributes().get("trackColor");
        String handleWidth = (String)getAttributes().get("handleWidth");
        String handleHeight = (String)getAttributes().get("handleHeight");
        String handleColor = (String)getAttributes().get("handleColor");
        String trackId = id+"_track";
        String handleId = id+"_handle";

        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("div", this);
        writer.writeAttribute("id", trackId, null);
        writer.writeAttribute("style", "width:"+trackWidth+";height:"+trackHeight+";position:relative;background:"+trackColor, null);
        writer.startElement("div", this);
        writer.writeAttribute("id", handleId, null);
        writer.writeAttribute("style", "width:"+handleWidth+";height:"+handleHeight+";position:absolute;background:"+handleColor+";cursor:move", null);
        writer.endElement("div");
        writer.endElement("div");
        writer.startElement("script", this);
        writer.writeAttribute("type", "text/javascript", null);
        String options = "{sliderValue:"+value+", range:$R("+minimumValue+","+maximumValue+")}";
        String script = id+" = new Control.Slider($('"+handleId+"'), $('"+trackId+"'), "+options+")";
        writer.writeText(script, null);
        writer.endElement("script");


        if (!context.getAttributes().containsKey("yearSliderId"))
            context.getAttributes().put("yearSliderId", id);
        else
            context.getAttributes().put("daySliderId", id);
    }

    @Override
    public String getFamily() {
        return "Rescriptus";
    }
}
